package sg.nus.iss.vmcs.gui;

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 * Fault Manager for creation of custom faults for the VMCS simulation<br>
 * @author Pang Pingli
 */
public class FaultManager extends Dialog implements ItemListener {

	//TODO: See if this can be refactored to state pattern
	/** Generated Serial ID	 */
	private static final long serialVersionUID = 4882953294974229226L;
	public 	static final char CHANGEGIVER 	= 'a';
	public 	static final char STORECOIN 	= 'b';
	public 	static final char DISPENSE 		= 'c';
	public 	static final char RECEIVECOIN	= 'd';
	public 	static final char NON 			= 'e';

	private  char currentFault =  NON;
	private  Checkbox  gc, dis, sc, non;

	public FaultManager(Frame fr) {
	    super(fr, "Fault Manager", false);
	    this.setLayout(new BorderLayout());
	    Label  lb = new Label ("Fault may occure at:");
	    this.add("North", lb);
	
	    CheckboxGroup cbg = new CheckboxGroup();
	    gc = new Checkbox("Give Changes", cbg, false); //initial state: off (false)
	    gc.addItemListener(this);
	    dis = new Checkbox("Dispense", cbg, false); //initial state: off
	    dis.addItemListener(this);
	    sc = new Checkbox("Store Coin", cbg, false); //initial state: off
	    sc.addItemListener(this);
	    non = new Checkbox("Non", cbg, true); //initial state: on
	    non.addItemListener(this);
	
	    Panel  pp = new Panel();
	    pp.setLayout(new GridLayout(0,1));
	    pp.add(gc);
	    pp.add(dis);
	    pp.add(sc);
	    pp.add(non);
	
	    this.add("Center", pp);
	    this.pack();
	}

	public void display(){
		this.setVisible(true);
	}

	public char  getCurrentFault(){
		return currentFault;
	}

	public void itemStateChanged(ItemEvent e) {
	    Checkbox  cb;
	
	    System.out.println("FaultManager.itemStateChanged:"+ e.getItem());
	    cb = (Checkbox) e.getSource();
	    if(cb == gc) {
	      currentFault = CHANGEGIVER;
	      return;
	    }
	    if(cb == dis) {
	      currentFault = DISPENSE;
	      return;
	    }
	
	    if(cb == sc) {
	      currentFault = STORECOIN;
	      return;
	    }
	
	    if(cb == non) {
	      currentFault = NON;
	      return;
	    }
	
	    return;
	}
}